package zjc.bookkeeping.minepage.aAssetaccount;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;
import zjc.bookkeeping.R;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.db.MethodBean;

public class AssetaccountActivity<list> extends Activity {

    private ListView listView;
    private TextView account;

    PtrFrameLayout ptrFrameLayout;
    DecimalFormat df = new DecimalFormat("#0.00");// 保留两位小数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assetaccount);
        //资产账户列表
        aslist();
        account=findViewById(R.id.asac);
        ptrFrameLayout = findViewById(R.id.ptr_frame_layout);


        getTotal();
        ptrnew();
    }

    //前往新增页面
    public void goadd(View v){
        Intent intentq = new Intent(this, AssetAddActivity.class);
        startActivity(intentq);
    }
    //返回侧边导航栏
    public void back(View v){
        finish();
//        // Intent 去设置要跳转的页面
//        Intent intent = new Intent(this, MainActivity.class);
//        // 进行跳转
//        startActivity(intent);
    }
    //账本数据写活
    private List<MethodBean> DataWriteAlivelist(){
        List<MethodBean> list = DBManager.getAllMethod();
        int k= 0 ;
        return list;
    }

    private String[] DataWriteAliveString(String s){
        List<MethodBean> list = DBManager.getAllMethod();
        int k= 0 ;
        String[] title = new String[list.size()];
        String[] accouunt = new String[list.size()];
        String[] card = new String[list.size()];
        String[] remark = new String[list.size()];

        if (s.equals(title)){
            return title;
        }else if (s.equals(accouunt)){
            return accouunt;
        }else if (s.equals(card)){
            return card;
        }else if (s.equals(remark)){
            return remark;
        }
        return title;
    }
    private int[] accountListImgId(){
        List<MethodBean> methodBeanList = new ArrayList<>();
        methodBeanList.addAll(DataWriteAlivelist());
        int k = methodBeanList.size();
        int[] imgId=new int[k];
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++){
            switch (methodBeanList.get(i).getMethod_image()){
                case "a_list_xj":
                    imgId[i]= R.drawable.a_list_xj;
                    break;
                case "a_list_zfb":
                    imgId[i]= R.drawable.a_list_zfb;
                    break;
                case "a_list_wx":
                    imgId[i]= R.drawable.a_list_wx;
                    break;
                case "a_list_yhk":
                    imgId[i]= R.drawable.a_list_yhk;
                    break;
                case "a_list_qqqb":
                    imgId[i]= R.drawable.a_list_qqqb;
                    break;
                case "a_list_other":
                    imgId[i]= R.drawable.a_list_other;
                    break;
            }
        }
        return imgId;
    }
    private int[] accountListAccountid(){
        List<MethodBean> methodBeanList = new ArrayList<>();
        methodBeanList.addAll(DataWriteAlivelist());
        int k = methodBeanList.size();
        int[] accountid=new int[k];
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++){
            accountid[i]=methodBeanList.get(i).getMethod_id();
        }
        return accountid;
    }
    private String[] accountListTitle(){
        List<MethodBean> methodBeanList = new ArrayList<>();
        methodBeanList.addAll(DataWriteAlivelist());
        int k = methodBeanList.size();
        String[] title = new String[k];
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++){
            String name = methodBeanList.get(i).getMethod_name();
            title[i] =  name;
        }
        return title;
    }
    private String[] accountListAccouunt(){
        List<MethodBean> methodBeanList = new ArrayList<>();
        methodBeanList.addAll(DataWriteAlivelist());
        int k = methodBeanList.size();
        String[] accouunt = new String[k];
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++){
            accouunt[i] = String.valueOf(methodBeanList.get(i).getMethod_money());
        }
        return accouunt;
    }
    private String[] accountListCard(){
        List<MethodBean> methodBeanList = new ArrayList<>();
        methodBeanList.addAll(DataWriteAlivelist());
        int k = methodBeanList.size();
        String[] card = new String[k];
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++){
           card[i]=methodBeanList.get(i).getMethod_card();
         }
        return card;
    }
    private String[] accountListRemark(){
        List<MethodBean> methodBeanList = new ArrayList<>();
        methodBeanList.addAll(DataWriteAlivelist());
        int k = methodBeanList.size();
        String[] remark = new String[k];
        Log.i("1.k", String.valueOf(k));
        for (int i = 0;i<k;i++){
            remark[i]=methodBeanList.get(i).getMethod_remark();
        }
        return remark;
    }
    private List<Map<String, Object>> getData() {
        List<MethodBean> methodBeanList = new ArrayList<>();
        methodBeanList.addAll(DataWriteAlivelist());
        int k = methodBeanList.size();
        Log.i("2.k=", String.valueOf(k));
        List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
        int[] imgId=accountListImgId();
        String[] title = accountListTitle();
        String[] accouunt = accountListAccouunt();
        String[] card = accountListCard();
        String[] remark = accountListRemark();
        for (int i = 0; i <k ; i++) {
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("image",imgId[i]);
            map.put("title",title[i]);
            map.put("account",accouunt[i]);
            map.put("card",card[i]);
            map.put("remark",remark[i]);
            list.add(map);
        }
        return list;

    }
    public double getTotal(){
        double allac=0.0;
        String[] accouunt = accountListAccouunt();
        for(int i=0;i< accouunt.length;i++){
            allac= allac+ Double.parseDouble(accouunt[i]);
            System.out.println("allac="+allac);
        }
        //String all = String.valueOf(allac);
        account.setText(df.format(allac));
        return allac;
    }
//    资产账户列表
    public void aslist(){
        listView= (ListView) findViewById(R.id.aslist);
        List<Map<String,Object>> list=getData();
        listView.setAdapter(new accountListAdapter(this,list));
        ViewTreeObserver vto = listView.getViewTreeObserver();
        // 根据列表高度动态改变报表高度
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                int h = listView.getHeight();
                Log.i(TAG, "Height=" + h); // 得到正确结果
                ViewGroup.LayoutParams params = listView.getLayoutParams();
                //params.width = width;
                params.height = h + 1000;
                listView.setLayoutParams(params);
                // cardView3.setLayoutParams();

                // 成功调用一次后，移除 Hook 方法，防止被反复调用
                // removeGlobalOnLayoutListener() 方法在 API 16 后不再使用
                // 使用新方法 removeOnGlobalLayoutListener() 代替
                listView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        //列表点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            int[] accountid=accountListAccountid();
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Intent intent = new Intent();
                intent.setClass(AssetaccountActivity.this, AssetaccountdetailActivity.class);
                //获取点击的数据
                HashMap<String, Object>itemMap = (HashMap<String, Object>) parent.getItemAtPosition(position);
                intent.putExtra("index", "" + position);
                intent.putExtra("accountid",accountid[position]);
                System.out.println("1.accountid="+accountid[position]+" "+position);
                intent.putExtra("title", "" + itemMap.get("title"));
                intent.putExtra("account", "" + itemMap.get("account"));
                startActivity(intent);
            }

        });
    }

    //下拉刷新
    public void ptrnew(){
        View root = LayoutInflater.from(this).inflate(R.layout.ptr_frame_header, ptrFrameLayout, false);
        View header = root.findViewById(R.id.header);
        ptrFrameLayout.setHeaderView(header);
        ptrFrameLayout.setPtrHandler(new PtrHandler() {
            //该方法用来设置下拉的条件
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                //如果content也就是我们的内容组件不能下滑，就可以进行下拉刷新
                return !content.canScrollVertically(-1);
            }

            //该方法在下拉开始的时候调用
            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                //设置一个两秒钟后完成刷新的动作
                header.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //设置刷新已完成，也就会关闭Loading
                        frame.refreshComplete();
                        onCreate(null);
                    }
                },2000);
            }
        });
        ptrFrameLayout.addPtrUIHandler(new PtrUIHandler() {
            @Override
            public void onUIReset(PtrFrameLayout frame) {

            }

            @Override
            public void onUIRefreshPrepare(PtrFrameLayout frame) {

            }

            //开始刷新的回调
            @Override
            public void onUIRefreshBegin(PtrFrameLayout frame) {
                //设置HeaderView（ProgressBar）开始转动
                if (frame.getHeaderView()!=null && frame.getHeaderView() instanceof ProgressBar){
                    ((ProgressBar)frame.getHeaderView()).setIndeterminate(true);
                }
            }

            //刷新结束的回调
            @Override
            public void onUIRefreshComplete(PtrFrameLayout frame) {
                //设置HeaderView（ProgressBar）停止转动
                if (frame.getHeaderView()!=null && frame.getHeaderView() instanceof ProgressBar){
                    ((ProgressBar)frame.getHeaderView()).setIndeterminate(false);
                }
            }

            @Override
            public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {

            }
        });

    }

}